//
//  BVPhoneLoginViewController.m
//  AFNetworking
//
//  Created by 崔晋 on 2019/10/12.
//

#import "BVPhoneLoginViewController.h"
#import "BVUIBizKitUtil.h"
#import "Masonry.h"
#import "SVProgressHUD.h"
#import "BVService.h"
#import "BVUserDefault.h"

@interface BVPhoneLoginViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *backImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *right;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *left;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextF;
@property (weak, nonatomic) IBOutlet UITextField *codeNumberTextF;
@property (weak, nonatomic) IBOutlet UIButton *getQrCodeButton;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSInteger second;

@end

@implementation BVPhoneLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
}

- (IBAction)loginAct:(id)sender {
    NSString *phoneNumber = self.phoneNumberTextF.text;
    NSString *qrcode = self.codeNumberTextF.text;
    if (phoneNumber.length == 0 || qrcode.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入完整"];
        return;
    }
    
    
    
    if (self.unionid) {
        [SVProgressHUD show];
        [[BVService shareInstance] post:@"wechat/register" requestBodt:@{@"phone": phoneNumber, @"code":qrcode, @"source": @"app", @"union_id": self.unionid} complete:^(BOOL success, id  _Nullable response) {
            if (success) {
                [SVProgressHUD dismiss];
                [BVUser sharedInstanceWith:response];
                [[BVUserDefault standardUserDefaults] setBool:YES forKey:BV_LOGIN_USER_DEFAULT_KEY];
                [[NSNotificationCenter defaultCenter] postNotificationName:BV_NOTIFICATION_LOGIN_SUCCESS_KEY object:nil];
            }else {
                [SVProgressHUD showErrorWithStatus:@"登录异常"];
            }
        }];
    }else {
        [SVProgressHUD show];
        [[BVService shareInstance] post:@"phone/login" requestBodt:@{@"phone": phoneNumber, @"code":qrcode, @"source": @"app"} complete:^(BOOL success, id  _Nullable response) {
            if (success) {
                [SVProgressHUD dismiss];
                [BVUser sharedInstanceWith:response];
                [[BVUserDefault standardUserDefaults] setBool:YES forKey:BV_LOGIN_USER_DEFAULT_KEY];
                [[NSNotificationCenter defaultCenter] postNotificationName:BV_NOTIFICATION_LOGIN_SUCCESS_KEY object:nil];
            }else {
                [SVProgressHUD showErrorWithStatus:@"登录异常"];
            }
        }];
    }
    
}

- (IBAction)getQrCodeAct:(UIButton *)sender {
    NSString *phoneNumber = self.phoneNumberTextF.text;
    if (phoneNumber.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if (self.second > 0) {
        return;
    }
    [SVProgressHUD show];
    [[BVService shareInstance] get:@"code" param:@{@"phone": phoneNumber} complete:^(BOOL success, id  _Nullable response) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发送成功"];
            self.second = 60;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAct:) userInfo:nil repeats:YES];
            [self timerAct:self.timer];
        }else {
            [SVProgressHUD showErrorWithStatus:@"发送失败"];
        }
    }];
}

- (void)timerAct:(NSTimer *)sender {
    self.second--;
    [self.getQrCodeButton setTitle:[NSString stringWithFormat:@"%ld秒重新获取", (long)self.second] forState:UIControlStateNormal];
    if (self.second == 0) {
        [sender invalidate];
        [self.getQrCodeButton setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
    }
}


- (void)setupViews {
    UIImage *backgroundImage = [BVUIBizKitUtil commonImageWithName:@"background_image"];
    self.backImageView.image = backgroundImage;
   
    UIImage *image = [UIImage bv_imageNamed:@"phone_login" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_USER_LOGIN_SCENE_MODULE]];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:image];
    [self.phoneNumberTextF setLeftView:imageV];
    UIImage *image1 = [UIImage bv_imageNamed:@"qr_code_login" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_USER_LOGIN_SCENE_MODULE]];
    UIImageView *imageV1 = [[UIImageView alloc] initWithImage:image1];
    [self.codeNumberTextF setLeftView:imageV1];
    
}



@end
